package com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.utils;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.yonyou.iuap.ipaas.connector.sdk.exception.BusinessModuleException;
import com.yonyou.iuap.ipaas.connector.sdk.exception.CommonErrorTypeDefinition;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;
import java.util.Map;

public class HttpClientUtils {
	public static String doGet(String host, String url, Map<String, String> headers, Map<String, Object> query, int timeout) throws BusinessModuleException {
		return doRequest(host, url, headers, query, null, Method.GET, timeout);
	}
	
	public static String doPost(String host, String url, Map<String, String> headers, Map<String, Object> query, Object body, int timeout) throws BusinessModuleException {
		return doRequest(host, url, headers, query, body, Method.POST, timeout);
	}
	
	public static String doPatch(String host, String url, Map<String, String> headers, Map<String, Object> query, Object body, int timeout) throws BusinessModuleException {
		return doRequest(host, url, headers, query, body, Method.PATCH, timeout);
	}
	
	public static String doPut(String host, String url, Map<String, String> headers, Map<String, Object> query, Object body, int timeout) throws BusinessModuleException {
		return doRequest(host, url, headers, query, body, Method.PUT, timeout);
	}
	
	private static String doRequest(String host, String url, Map<String, String> headers, Map<String, Object> query, Object body, Method method, int timeout) throws BusinessModuleException {
		String resultStr;
		url = dealRequestUrl(host, url);
		String formUrl = MapUtils.isEmpty(query) ? url : HttpUtil.urlWithForm(url, query, Charset.defaultCharset(), false);
		String bodyStr = JSONUtil.toJsonStr(body);
		HttpRequest httpRequest = ((HttpRequest) HttpUtil.createRequest(method, formUrl).addHeaders(headers)).timeout(timeout);
		if (Method.POST.equals(method) || Method.PUT.equals(method) || Method.PATCH.equals(method))
			httpRequest.body(bodyStr);
		LogUtils.apiRequestInfo(formUrl, bodyStr);
		try {
			resultStr = httpRequest.execute().body();
		} catch (Exception e) {
			LogUtils.printStacktrace(e);
			throw BusinessModuleException.simpleException("999", "http request error,errorMsg:" + e.getMessage(), CommonErrorTypeDefinition.CONNECTIVITY);
		}
		String logResult = (StringUtils.isNotBlank(resultStr) && resultStr.length() > 3000) ? resultStr.substring(0, 3000) : resultStr;
		LogUtils.apiResponseInfo(formUrl, logResult);
		return resultStr;
	}
	
	public static String doDelete(String host, String url, Map<String, String> headers, Map<String, Object> query, Object body, int timeout) throws BusinessModuleException {
		String resultStr;
		url = dealRequestUrl(host, url);
		String formUrl = MapUtils.isEmpty(query) ? url : HttpUtil.urlWithForm(url, query, Charset.defaultCharset(), false);
		String bodyStr = JSONUtil.toJsonStr(body);
		LogUtils.apiRequestInfo(formUrl, bodyStr);
		HttpRequest httpRequest = ((HttpRequest) HttpRequest.delete(formUrl).addHeaders(headers)).timeout(timeout);
		if (StringUtils.isNotEmpty(bodyStr))
			httpRequest.body(bodyStr);
		try {
			resultStr = httpRequest.execute().body();
		} catch (HttpException e) {
			LogUtils.printStacktrace((Exception) e);
			throw BusinessModuleException.simpleException("999", "http request error,errorMsg:" + e.getMessage(), CommonErrorTypeDefinition.CONNECTIVITY);
		}
		String logResult = (StringUtils.isNotBlank(resultStr) && resultStr.length() > 3000) ? resultStr.substring(0, 3000) : resultStr;
		LogUtils.apiResponseInfo(formUrl, logResult);
		return resultStr;
	}
	
	private static String dealRequestUrl(String host, String url) {
		return StringUtils.endsWith(host, "/") ? (host.substring(0, host.length() - 1) + url) : (host + url);
	}
}